package ext.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.ReferenceFactory;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;
import wt.vc.VersionControlHelper;

public class RestDemoHelper {
	
	static ReferenceFactory rf = new ReferenceFactory();
	
	/**
	 * 获取系统用户
	 * @return
	 */
	public static List<EhCacheDTO> getWCUsers(){
		List<EhCacheDTO> users=new ArrayList<EhCacheDTO>();
		try{
			QuerySpec qs = new QuerySpec(WTUser.class);
		    QueryResult rs = PersistenceHelper.manager.find(qs);
		    while (rs.hasMoreElements()) {
		    	WTUser user = (WTUser) rs.nextElement();
		    	String userIda2a2="wt.org.WTUser:"+user.getPersistInfo().getObjectIdentifier().getId();
		    	String userName=user.getName();
		    	//logger.debug(">>WCUser>>userIda2a2:"+userIda2a2+">>userName:"+userName);
	    	    EhCacheDTO ehCache=new EhCacheDTO();
                ehCache.setId(userIda2a2);
                Map<String,String> attributes= new HashMap<String,String>();
                attributes.put("name", userName);
                ehCache.setAttributes(attributes);
		    	users.add(ehCache);
		    }
		}catch(Exception e){
			System.out.print(e.getMessage());
		}
		return users;
	}
	

	/**
	 * 根据part的编号获取对应的WTPartMaster
	 */
	public static WTPartMaster getWTPartMasterBYNumber(String number)
	
	{
		WTPartMaster master = null;
		try{
			
			
			QuerySpec qsMaster = new QuerySpec(WTPartMaster.class);
			SearchCondition sc = new SearchCondition(WTPartMaster.class, WTPartMaster.NUMBER, SearchCondition.EQUAL, number);
			qsMaster.appendSearchCondition(sc);
			QueryResult qrMaster = PersistenceHelper.manager.find(qsMaster);
			if(qrMaster.hasMoreElements()){
				
				master = (WTPartMaster) qrMaster.nextElement();
			}
			
		}catch (WTException e) {
			e.printStackTrace();
		} 
		return master;
		
	}

	/**
	 * 根据WTPartMaster获得最新版WTPart
	 * 
	 */
	public static WTPart getLatestPart(WTPartMaster partMaster)
			throws Exception {
		WTPart part = null;
		if (partMaster != null) {
			QueryResult qr = VersionControlHelper.service
					.allVersionsOf(partMaster);
			if (qr != null && qr.hasMoreElements()) {
				part = (WTPart) qr.nextElement();
			}
		}
		return part;
	}
	
	public static List<EhCacheDTO> getPart(WTPart part){
		List<EhCacheDTO> partInfo=new ArrayList<EhCacheDTO>();
		try{
			 EhCacheDTO ehCache=new EhCacheDTO();
			 String oid = rf.getReferenceString(part);
             ehCache.setId(oid);
             Map<String,String> attributes= new HashMap<String,String>();
             attributes.put("name", part.getName());
             attributes.put("number", part.getNumber());
             attributes.put("creator", part.getCreatorFullName());
             ehCache.setAttributes(attributes);
             partInfo.add(ehCache);
		}catch(Exception e){
			System.out.print(e.getMessage());
		}
		return partInfo;
	}
	
}
